﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace SPThumbnailExtender.Interne
{
    internal class ThumbnailWMV : ThumbnailBase
    {
        public ThumbnailWMV(Uri FileURI)
            : base(FileURI)
        {

        }

        public ThumbnailWMV(Uri FileURI, int Width, int Height)
            : base(FileURI, Width, Height)
        {

        }


        public override System.Drawing.Bitmap GenerateThumbnail()
        {
            return GenerateThumbnail(1);
        }

        public override System.Drawing.Bitmap GenerateThumbnail(int Page)
        {
            string strPathPNG = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, 0, ".png");
            FileInfo file = new FileInfo(strPathPNG);
            if (!file.Directory.Exists)
                file.Directory.Create();
            string strPathWmv = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, 0, ".wmv");
            byte[] byteFile = Helper.ReadToEnd(this.GetFileStream());
            File.WriteAllBytes(strPathWmv, byteFile);
            Bitmap image;
            using (AsfMojo.File.AsfFile asfFile = new AsfMojo.File.AsfFile(strPathWmv))
            {
                strPathPNG = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, Page, ".png");
                image = AsfMojo.Media.AsfImage.FromFile(strPathWmv, Page);
                Helper.DeleteThumbnail(this.FileUri, Page);
                Helper.InsertThumbnail(this.FileUri, Page, image);
                File.Delete(strPathWmv);
            }
            return image;
        }

        public override int GetCount()
        {
            string strPathPNG = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, 0, ".png");
            FileInfo file = new FileInfo(strPathPNG);
            if (!file.Directory.Exists)
                file.Directory.Create();
            string strPathWmv = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, 0, ".wmv");
            byte[] byteFile = Helper.ReadToEnd(this.GetFileStream());
            File.WriteAllBytes(strPathWmv, byteFile);
            int duration;
            using (AsfMojo.File.AsfFile asfFile = new AsfMojo.File.AsfFile(strPathWmv))
            {
                AsfMojo.Parsing.AsfFileProperties FileProperties = asfFile.GetAsfObject<AsfMojo.Parsing.AsfFileProperties>();
                duration = ((int)FileProperties.Duration.TotalSeconds - 1);
                File.Delete(strPathWmv);
            }
            int iOffset = 15;
            if (duration > 120)
                iOffset = duration / 10;
            return duration/iOffset;
        }

        public override void GenerateThumbnails()
        {
            string strPathPNG = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, 0, ".png");
            FileInfo file = new FileInfo(strPathPNG);
            if (!file.Directory.Exists)
                file.Directory.Create();
            string strPathWmv = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, 0, ".wmv");
            byte[] byteFile = Helper.ReadToEnd(this.GetFileStream());
            File.WriteAllBytes(strPathWmv, byteFile);
            using (AsfMojo.File.AsfFile asfFile = new AsfMojo.File.AsfFile(strPathWmv))
            {
                AsfMojo.Parsing.AsfFileProperties FileProperties = asfFile.GetAsfObject<AsfMojo.Parsing.AsfFileProperties>();
                int duration = ((int)FileProperties.Duration.TotalSeconds - 1);
                int iCurrent = 0;
                int iOffset = 15;
                if(duration > 120)
                    iOffset=duration / 10;
                Helper.DeleteThumbnailsForDocument(this.FileUri);
                object o = Helper.GetParameter("SPThumbnailExtender.NbPage");
                int iNBPage;
                if (o == null)
                    iNBPage = 0;
                else
                    int.TryParse(o.ToString(), out iNBPage);


                for (int miind = 1; miind < duration; miind += iOffset)
                {
                    if (iNBPage > 0 && miind > iNBPage)
                    {
                        break;
                    }

                    strPathPNG = Helper.GetSourceFilename(this.FileUri.AbsoluteUri, 0, 0, miind, ".png");
                    try
                    {
                        using (Bitmap image = AsfMojo.Media.AsfImage.FromFile(strPathWmv, miind))
                        {
                            if (image == null)
                                continue;
                            Helper.InsertThumbnail(this.FileUri, iCurrent, image);
                            iCurrent++;

                        }
                    }
                    catch
                    { }
                }
                File.Delete(strPathWmv);
            }

        }
    }
}
